package com.pay.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.ClobStringType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 反射工具类
 * Created by Administrator on 2017-7-7.
 */
public final class ReflectionUtils {
    private static final Logger logger = LoggerFactory.getLogger(ReflectionUtils.class);

    /**
     * 创建类实例
     * @param cls
     * @return
     */
    public static Object getInstance(Class<?> cls){
        Object instance = new Object();
        try {
            instance = cls.newInstance();
        }catch (Exception ex){
            logger.error("new instance class failure！",ex);
            throw new RuntimeException(ex);
        }
        return instance;
    }

    /**
     * 调用方法
     * @param obj
     * @param method
     * @param args
     * @return
     */
    public static Object invokeMethod(Object obj, Method method, Object... args){
        Object result = new Object();
        try {
            method.setAccessible(true);
            result = method.invoke(obj,args);
        }catch (Exception ex){
            logger.error("invoke method failure！",ex);
            throw new RuntimeException(ex);
        }
        return result;
    }

    /**
     * 设置参数
     * @param obj
     * @param field
     * @param value
     */
    public static void setField(Object obj, Field field,Object value){
        try {
            field.setAccessible(true);
            field.set(obj,value);
        }catch (Exception ex){
            logger.error("set field failure！",ex);
            throw new RuntimeException(ex);
        }
    }
}
